//
//  XPhoto.m
//  XPhotoLibraryKit
//
//  Created by Wu,Xinting on 2018/3/21.
//

#import "XPhoto.h"

static NSString *kSize = @"kSize";
static NSString *kDate = @"kDate";
static NSString *kID = @"kID";
static NSString *kType = @"kType";
static NSString *kLocation = @"kLocation";
static NSString *kDuration = @"kDuration";
@interface XPhoto ()

@property (nonatomic, assign) CGSize size;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, copy) NSString *ID;
@property (nonatomic, assign) PHAssetMediaType type;
@property (nonatomic, strong) CLLocation *location;
@property (nonatomic, assign) NSTimeInterval duration;

@end

@implementation XPhoto

- (instancetype)init {
    if (self = [super init]) {
        _transform = CGAffineTransformIdentity;
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeCGSize:self.size forKey:kSize];
    [aCoder encodeObject:self.date forKey:kDate];
    [aCoder encodeObject:self.ID forKey:kID];
    [aCoder encodeInteger:(NSInteger)self.type forKey:kType];
    [aCoder encodeObject:self.location forKey:kLocation];
    [aCoder encodeDouble:self.duration forKey:kDuration];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [self init]) {
        self.size = [aDecoder decodeCGSizeForKey:kSize];
        self.date = [aDecoder decodeObjectForKey:kDate];
        self.ID = [aDecoder decodeObjectForKey:kID];
        self.type = [aDecoder decodeIntegerForKey:kType];
        self.location = [aDecoder decodeObjectForKey:kLocation];
        self.duration = [aDecoder decodeDoubleForKey:kDuration];
    }
    return self;
}

#pragma mark - Get&Set
- (void)setAsset:(PHAsset *)asset {
    _asset = asset;
    
    self.size = CGSizeMake(asset.pixelWidth, asset.pixelHeight);
    self.ID = asset.localIdentifier;
    self.date = asset.creationDate;
    self.type = asset.mediaType;
    self.location = asset.location;
    self.duration = asset.duration;
}

@end
